/*package com.gohi.main.wxapi.service.impl;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.gohi.main.util.EmojiFilter;
import com.gohi.main.util.Utils;
import com.gohi.main.wxaccount.hbm.WxAccount;
import com.gohi.main.wxaccount.service.IWxAccountService;
import com.gohi.main.wxapi.MsgType;
import com.gohi.main.wxapi.process.MsgXmlUtil;
import com.gohi.main.wxapi.process.WxApi;
import com.gohi.main.wxapi.service.IwxControl;
import com.gohi.main.wxapi.service.WxCmsService;
import com.gohi.main.wxapi.util.AccoutUtil;
import com.gohi.main.wxapi.vo.MsgNews;
import com.gohi.main.wxapi.vo.MsgRequest;
import com.gohi.main.wxfensi.hbm.WxFensi;
import com.gohi.main.wxfensi.service.IWxFensiService;
import com.gohi.main.wxmsgbase.hbm.WxMsgbase;
import com.gohi.main.wxmsgbase.service.IWxMsgbaseService;
import com.gohi.main.wxmsgnews.hbm.WxMsgnews;
import com.gohi.main.wxmsgnews.service.IWxMsgnewsService;


*//**
 * 微信处理层
 * @author Administrator
 *
 *//*
public class WxControlimpl implements IwxControl {
	Logger log = Logger.getLogger(WxApiProcessImpl.class);
	private WxCmsService wxCmsService;
	//粉丝信息
	private IWxFensiService wxFensiService;
	private WxAccount acc;
	//消息触发的关键
	private IWxMsgbaseService wxMsgbaseService;
	private WxMsgbase msg;
	//微信公众号
	private IWxAccountService wxAccountService;
	
	//图文消息
	private IWxMsgnewsService wxMsgnewsService;
	
	public String processMsg(MsgRequest msgRequest,String appId,String appSecret){
		if(msgRequest==null){
			log.equals("msgRequest参数错误");
			return null;
		}
		String msgtype = msgRequest.getMsgType();
		String respXml = null;
		if(msgtype.equals(MsgType.Text.toString())){//文本消息
			respXml = this.processTextMsg(msgRequest,appId,appSecret);
		}else if(msgtype.equals(MsgType.Image.toString())){//图片消息
			
		}else if(msgtype.equals(MsgType.Location.toString())){//地理位置消息
			
		}else if(msgtype.equals(MsgType.Event.toString())){//事件消息
			respXml = this.processEventMsg(msgRequest);
		}
		
		//为空，返回 subscribe 消息
		if(StringUtils.isEmpty(respXml)){
		
			 acc =AccoutUtil.getAccountByid(msgRequest.getUuid());//取当前会话的ID
			 //开始获取粉丝
			 saveAccountFans(msgRequest.getFromUserName(),acc.getAppid(),acc.getAppsecret());//保存粉丝信息
			 msg =new WxMsgbase();
			 msg.setTexts("/大兵 欢迎关注"); //暂时写死
			 
				if(msg != null){
					respXml= MsgXmlUtil.textToXml(wxCmsService.getMsgResponseText(msgRequest, msg));
				}
		}
		
		try {
			if(respXml!=null){
			return new String(respXml.getBytes("GBK"),"GB2312");//linux需要进行转码
			}else{
				return null;
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return "error";
		}
	}
	
	//处理文本消息
	private String processTextMsg(MsgRequest msgRequest,String appId,String appSecret){
		String content = msgRequest.getContent();
		if(!StringUtils.isEmpty(content)){//文本消息
			String tmpContent = content.trim();//获取到用户输入的文字
			 msg = wxMsgbaseService.getlikemsg(msgRequest.getUuid(), tmpContent);
			 if(msg==null){
				 msg =new WxMsgbase();
				 msg.setTexts("很高兴看到你的信息，请您参照公众号底部菜单进行操作。");
				 return MsgXmlUtil.textToXml(wxCmsService.getMsgResponseText(msgRequest, msg));
	    	}else{
	    		 //这里我写死了  暂时没发送文本消息
				 if(msg.getMsgtype().equals("news")){
					 //如果是新闻
					 List<WxMsgnews> msgNews = wxMsgnewsService.getByBaseId(msg.getNewsid());
					 return MsgXmlUtil.newsToXml(wxCmsService.getMsgResponseNews(msgRequest, msgNews));
				 }else{
					 return MsgXmlUtil.textToXml(wxCmsService.getMsgResponseText(msgRequest, msg));
				 }
			
	    	}
			 
			
			
		
		}
		return null;
	}
	
	//处理事件消息
	private String processEventMsg(MsgRequest msgRequest){
		String key = msgRequest.getEventKey();
		if(MsgType.SUBSCRIBE.toString().equals(msgRequest.getEvent())){//订阅消息
			 acc =AccoutUtil.getAccountByid(msgRequest.getUuid());//取当前会话的ID
			 //开始获取粉丝
			 saveAccountFans(msgRequest.getFromUserName(),acc.getAppid(),acc.getAppsecret(),acc.getId(),acc.getAccess_token());//保存粉丝信息
			
			 msg =new WxMsgbase();
			WxAccount wx= wxAccountService.getById(msgRequest.getUuid());
			if(acc.getAtype()==0){
				msg.setTexts(acc.getAutotext()); 
				if(msg != null){
					return MsgXmlUtil.textToXml(wxCmsService.getMsgResponseText(msgRequest, msg));
				}
			}else{
				 List<WxMsgnews> msgNews = wxMsgnewsService.getByBaseId(acc.getNewsid());
				 System.err.println(msgNews.get(0).getTitle());
				 //System.err.println(msg.getTexts());
				 return MsgXmlUtil.newsToXml(wxCmsService.getMsgResponseNews(msgRequest, msgNews));
			}
			
		
			 
			
		}else if(MsgType.UNSUBSCRIBE.toString().equals(msgRequest.getEvent())){//取消订阅
			// acc =AccoutUtil.getAccountByid(msgRequest.getUuid());//取当前会话的ID
			System.err.println("取消:"+msgRequest.getFromUserName());
			wxFensiService.updateSate(msgRequest.getFromUserName());
			
			//updateSate
			//wxFensiService
			 //saveAccountFans(msgRequest.getFromUserName(),acc.getAppid(),acc.getAppsecret());//取消订阅
				
			
		}else{//CLICK
			
			 msg =new WxMsgbase();
			 msg.setTexts("/大兵 暂时没开发,程序员正在努力codeing.."); //暂时写死
			if(msg != null){
				return MsgXmlUtil.textToXml(wxCmsService.getMsgResponseText(msgRequest, msg));
			}
			
			
			if(!StringUtils.isEmpty(key)){
				if(key.startsWith("_fix_")){//固定消息
					String baseIds = key.substring("_fix_".length());
					if(!StringUtils.isEmpty(baseIds)){
						String[] idArr = baseIds.split(",");
						if(idArr.length > 1){//多条图文消息
							
							List<MsgNews> msgNews = msgBaseDao.listMsgNewsByBaseId(idArr);
							if(msgNews != null && msgNews.size() > 0){
								return MsgXmlUtil.newsToXml(wxCmsService.getMsgResponseNews(msgRequest, msgNews));
							}
							
						}else{//图文消息，或者文本消息
							MsgBase msg = msgBaseDao.getById(baseIds);
							if(msg.getMsgtype().equals(MsgType.Text.toString())){
								
								MsgText text = msgBaseDao.getMsgTextByBaseId(baseIds);
								if(text != null){
									return MsgXmlUtil.textToXml(wxCmsService.getMsgResponseText(msgRequest, text));
								}
							
							}else{
								
								List<MsgNews> msgNews = msgBaseDao.listMsgNewsByBaseId(idArr);
								if(msgNews != null && msgNews.size() > 0){
									return MsgXmlUtil.newsToXml(wxCmsService.getMsgResponseNews(msgRequest, msgNews));
								}
							}
						}
					}
				}
			}
		}
		return null;
	}
	
	//获取用户信息接口 - 必须是开通了认证服务，否则微信平台没有开放此功能
	public void saveAccountFans(MsgRequest msgRequest, String appId, String appSecret){
		String token = WxApi.getToken(appId,appSecret).getAccessToken();
		String url = WxApi.getFansInfoUrl(token, msgRequest.getFromUserName());
		JSONObject jsonObject = WxApi.httpsRequest(url, "GET", null);
		System.out.println(jsonObject.toString());
		if (null != jsonObject) {
			try{
				wxCmsService.saveAccountFans(jsonObject);
			}catch(Exception e){
				if(jsonObject.containsKey("errcode")){
					int errorCode = jsonObject.getInt("errcode");
					String errorMsg = jsonObject.getString("errmsg");
					System.out.println(String.format("获取用户信息失败 errcode:{} errmsg:{}", errorCode, errorMsg));
				}
				e.printStackTrace();
			}
		}
	}
	public boolean saveAccountFans(String fromUserName, String appId, String appSecret,String parid,String token){
		//String token = WxApi.getToken(appId,appSecret).getAccessToken();
		String url = WxApi.getFansInfoUrl(token, fromUserName);
	
		JSONObject jsonObject = WxApi.httpsRequest(url, "GET", null);
		//System.out.println(jsonObject.toString());
		if (null != jsonObject) {
			try{
				//System.err.println(jsonObject.toString());
				WxFensi f =new WxFensi();
				f.setId(jsonObject.get("openid").toString());
				f.setCity(jsonObject.get("city").toString());
				f.setHeadimgurl(Utils.reshead(jsonObject.get("headimgurl").toString(), 132));
				//System.err.println(jsonObject.toString());
				System.err.println(new String(jsonObject.get("nickname").toString().getBytes(),"gb2312"));
				try{
				f.setNickname(EmojiFilter.filterEmoji(new String(jsonObject.get("nickname").toString().getBytes(),"utf-8")));
				f.setNickname(f.getNickname().replace("\\xF0", "x"));
				f.setNickname(f.getNickname().replace("\\x9F", "x"));
				f.setNickname(f.getNickname().replace("\\x98", "x"));
				f.setNickname(f.getNickname().replace("\\x8A", "x"));
				f.setNickname(f.getNickname().replace("\\xF0\\x9F\\x98\\x8A", "x"));
				
				System.err.println(f.getNickname());
				}catch(Exception e){
					System.err.println("出错啦!");
					f.setNickname("none");
				}
				f.setLanguage(jsonObject.get("language").toString());
				f.setProvince(jsonObject.get("province").toString());
				f.setSex(Integer.parseInt(jsonObject.get("sex").toString()));
				f.setOpenid(jsonObject.get("openid").toString());
				f.setSubscribeStatus(Integer.parseInt(jsonObject.get("subscribe").toString()));
				f.setRemark(jsonObject.get("remark").toString());
				f.setSubscribeTime(jsonObject.get("subscribe_time").toString());
				f.setParid(parid);
				f.setStatus(0);//
				f.setCreateTime(new Date());
				f.setCountry(jsonObject.get("country").toString());
				try{
				wxFensiService.saveOrUpdate(f);
				}catch(Exception e){
					System.err.println("出错啦!");
					//f.setNickname("none");
				}
				return true;
				//System.err.println("openid====="+jsonObject.get("openid"));
				//wxCmsService.saveAccountFans(jsonObject);
			}catch(Exception e){
				if(jsonObject.containsKey("errcode")){
					int errorCode = jsonObject.getInt("errcode");
					String errorMsg = jsonObject.getString("errmsg");
					System.out.println(String.format("获取用户信息失败 errcode:{} errmsg:{}", errorCode, errorMsg));
				}
				//e.printStackTrace();
			}
		}else{
			return false;
		}
		
		return false;
	}
	
	
	public WxCmsService getWxCmsService() {
		return wxCmsService;
	}

	public void setWxCmsService(WxCmsService wxCmsService) {
		this.wxCmsService = wxCmsService;
	}

	public IWxMsgbaseService getWxMsgbaseService() {
		return wxMsgbaseService;
	}

	public void setWxMsgbaseService(IWxMsgbaseService wxMsgbaseService) {
		this.wxMsgbaseService = wxMsgbaseService;
	}

	public WxMsgbase getMsg() {
		return msg;
	}

	public void setMsg(WxMsgbase msg) {
		this.msg = msg;
	}

	public IWxFensiService getWxFensiService() {
		return wxFensiService;
	}

	public void setWxFensiService(IWxFensiService wxFensiService) {
		this.wxFensiService = wxFensiService;
	}

	public WxAccount getAcc() {
		return acc;
	}

	public void setAcc(WxAccount acc) {
		this.acc = acc;
	}

	public IWxAccountService getWxAccountService() {
		return wxAccountService;
	}

	public void setWxAccountService(IWxAccountService wxAccountService) {
		this.wxAccountService = wxAccountService;
	}

	public IWxMsgnewsService getWxMsgnewsService() {
		return wxMsgnewsService;
	}

	public void setWxMsgnewsService(IWxMsgnewsService wxMsgnewsService) {
		this.wxMsgnewsService = wxMsgnewsService;
	}
	
	
}
*/